package com.zss.leetcode;

import java.util.HashMap;

/**
 * 3.无重复字符的最长子串
 * @author fuguozhang
 * @create 2022/5/15
 */
public class LengthOfLongestSubstring {

    public static void main(String[] args) {
        int res = lengthOfLongestSubstring("abcefgeuricdqwsb");
        System.out.println(res);
    }

    public static int lengthOfLongestSubstring(String s){
        int ans = 0;
        HashMap<Character,Integer> map = new HashMap<>();
        int start = 0;
        for(int end = 0;end < s.length();end++){
            char ch = s.charAt(end);
            if(map.containsKey(ch)){
                start = Math.max(map.get(ch)+1,start);
            }
            ans = Math.max(ans,end - start + 1);
            map.put(ch,end);
        }
        return ans;
    }
}
